﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleKit.other
{
    public class DatabaseTest
    {
        /*
         * PDL:
         * 
         */
        private const string parentDir = "C:\\workspace\\c\\my-lib\\sql\\main\\";
        private const string databaseScriptFile = "C:\\workspace\\c\\my-lib\\sql\\main\\CreateDatabase.sql";
        private const string sqlFile = parentDir + "sql.txt";
        private static SqlHelper sqlHelper = new SqlHelper();

        public static void DatabaseTestMain(string[] args)
        {
            TestDatabase();

            System.IO.StreamReader file =
                new System.IO.StreamReader(sqlFile);

            string line;
            while ((line = file.ReadLine()) != null)
            {
                List<string> output = ReadFileToString(parentDir + line);
                foreach (string sqlCmd in output)
                {
                    Console.WriteLine(sqlCmd);
                    System.Diagnostics.Trace.WriteLine(sqlCmd);
                    sqlHelper.CreateTable(sqlCmd);
                }
            }
            file.Close();
            sqlHelper.Dispose();
        }

        private static void TestDatabase()
        {
            List<string> output = ReadFileToString(databaseScriptFile);
            foreach (string sqlCmd in output)
            {
                Console.WriteLine(sqlCmd);
                System.Diagnostics.Trace.WriteLine(sqlCmd);
                sqlHelper.CreateDatabase(sqlCmd);
            }
        }

        private static List<string> ReadFileToString(string filePath)
        {
            System.IO.StreamReader file = new System.IO.StreamReader(filePath);

            string line;
            string temp = string.Empty;
            List<string> output = new List<string>();
            Regex r = new Regex("^GO");
            while ((line = file.ReadLine()) != null)
            {
                //.Replace("GO", "\r\n")
                if (r.IsMatch(line))
                {
                    output.Add(temp);
                    temp = string.Empty;
                    continue;
                }
                temp += line + "\r\n";
            }

            file.Close();
            return output;
        }

    }
}
